package com.tbm.console.proj.controller;

import com.alibaba.fastjson.JSONArray;
import com.tbm.console.base.controller.BaseController;
import com.tbm.console.common.constant.ConsoleConstant;
import com.tbm.console.proj.entity.LocalConstant;
import com.tbm.console.proj.entity.LocalUserInfoEntity;
import com.tbm.console.proj.entity.SystemMenu;
import com.tbm.console.proj.entity.SystemUser;
import com.tbm.console.proj.service.LocalConstantService;
import com.tbm.console.proj.service.LocalUserInfoService;
import com.tbm.console.proj.service.SystemMenuService;
import com.tbm.console.util.JsonUtils;
import com.tbm.console.util.JurisdictionUtil;
import com.tbm.console.util.PageData;
import com.tbm.console.util.RightsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

//说明 字符串：LocalUserInfo，作为替换字符串

@Controller
public class LocalUserInfoController extends BaseController {

    @Autowired
    private LocalUserInfoService localUserInfoService;


    @Autowired
    private SystemMenuService systemMenuService;


    @Autowired
    private LocalConstantService localConstantService;

    /**
     *
     *
     * @return
     */
    @RequestMapping("/localUserInfo-list")
    public ModelAndView listManage(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            viewName = "localUserInfo/localUserInfo-list";
            HttpSession session = this.getRequest().getSession();
            SystemUser systemUser = JsonUtils.jsonToObject(session.getAttribute(ConsoleConstant.SESSIONUSER).toString(), SystemUser.class);
            String rights = session.getAttribute(ConsoleConstant.RIGHTS).toString();

            PageData pageData = new PageData();
            pageData.put("userId", systemUser.getId());
            List<LocalUserInfoEntity> all = localUserInfoService.findAll(pageData);
            PageData result = new PageData();
            result.put("loginName", systemUser.getUsername());
            result.put("password", "");

            String checkBoxRights = "SS" ;
            if(all.size()==1){
                LocalUserInfoEntity localUserInfoEntity = all.get(0);
                result.put("nickname", localUserInfoEntity.getNickname());
                result.put("userId", localUserInfoEntity.getUserId());
                result.put("email", localUserInfoEntity.getEmail());
                result.put("short", localUserInfoEntity.getUserShortCut());
                result.put("develop", localUserInfoEntity.getDevelopPath());
                result.put("ftl", localUserInfoEntity.getDevelopFTLPath());
                checkBoxRights = (localUserInfoEntity.getUserShortCut() == null || localUserInfoEntity.getUserShortCut().equals("")) ? "SS" : localUserInfoEntity.getUserShortCut();
            }else{
                result.put("nickname", "");
                result.put("userId", systemUser.getId());
                result.put("email", "");
                result.put("short", "");
                result.put("develop", "");
                result.put("ftl", "");
            }
            PageData constantPd = new PageData();
            constantPd.put("constantKey", "group-types");
            List<LocalConstant> groupTypesTypes = localConstantService.findAll(constantPd);
            constantPd.put("constantKey","power-level-types");
            List<LocalConstant> powerLevelTypes = localConstantService.findAll(constantPd);
            modelAndView.addObject("powerLevelTypes",powerLevelTypes);
            modelAndView.addObject("groupTypes",groupTypesTypes);
            modelAndView.addObject("shortInfo", shortList(rights,checkBoxRights.substring(1)));
            modelAndView.addObject("userInfo", result);
        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    /**
     * 系统初始化的时候，需要验证的一些信息
     * @return
     */
    @RequestMapping("/localUserInfo-wizard")
    public ModelAndView wizardManage(){

        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            //获取Session里面的用户
            HttpSession session = this.getRequest().getSession();
            SystemUser systemUser = JsonUtils.jsonToObject(session.getAttribute(ConsoleConstant.SESSIONUSER).toString(), SystemUser.class);
            String rights = session.getAttribute(ConsoleConstant.RIGHTS).toString();
            PageData result = new PageData();
            result.put("loginName", systemUser.getUsername());
            result.put("userId", systemUser.getId());
            viewName = "localUserInfo/localUserInfo-wizard";
            modelAndView.addObject("shortInfo", shortList(rights,"S"));
            modelAndView.addObject("userInfo", result);

        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    //--------------------------------------------------------------------------//
    private List<PageData> shortList (String rights,String shortCuts){
        List<Integer> idsList = new ArrayList<>();
        List<PageData> resultList = new ArrayList<>();
        for(int i=0;i<200;i++){
            if (RightsHelper.testRights(rights.substring(1), i)) {
                idsList.add(i);
            }
        }
        Integer[] idsArray = new Integer[idsList.size()];
        idsList.toArray(idsArray);
        List<SystemMenu> allById = systemMenuService.findAllById(idsArray);
        for(SystemMenu menu : allById){
            if(menu.getParentId().equals("0")){
               continue;
            }
            PageData pageData = new PageData();
            pageData.put("menuId", menu.getId());
            pageData.put("menuName", menu.getMenuName());
            pageData.put("checkBoxName", "checkBoxName_" + menu.getId());
            String str = "unchecked";
            try {
                if(RightsHelper.testRights(shortCuts,menu.getId())){
                    str = "checked";
                }
            } catch (NumberFormatException e) {

            }

            pageData.put("check", str);
            resultList.add(pageData);
        }
        return resultList;
    }
}